package mx.com.crowdgine.display.animation.mocap;


import junit.framework.Assert;

import org.junit.Test;

public class MocapTextureEncoderTest {

	private MocapTextureEncoder createEncoder() {
		String asfFileName = "Assets/Mocap/12.asf";
		String amcFileName = "Assets/Mocap/12_01.amc";
		
		MocapTextureEncoder encoder = new MocapTextureEncoder();
		encoder.setAsfFile(new ASFFile(asfFileName));
		encoder.addAmcFile(new AMCFile(amcFileName));
		return encoder;
	}
	
	private float[] getData() {
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		return data;
	}
	
	
	
	@Test
	public void testRootOrderEncoding() {
		
		float[] data = getData();
		
		Assert.assertEquals(1.0f, data[0]);
		Assert.assertEquals(2.0f, data[1]);
		Assert.assertEquals(3.0f, data[2]);
		Assert.assertEquals(0.0f, data[3]);
		
		Assert.assertEquals(4.0f, data[4]);
		Assert.assertEquals(5.0f, data[5]);
		Assert.assertEquals(6.0f, data[6]);
		Assert.assertEquals(0.0f, data[7]);
		
		
	}


	
	@Test
	public void testRootAxisEncoding() {
		float [] data = getData();
		int offset = MocapTextureEncoder.ROOT_AXIS_OFFSET;
		
		Assert.assertEquals(1.0f, data[offset+0]);
		Assert.assertEquals(2.0f, data[offset+1]);
		Assert.assertEquals(3.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
	}
	
	
	@Test
	public void testRootPosition() {
		float [] data = getData();
		int offset = MocapTextureEncoder.ROOT_POSITION_OFFSET;
		
		Assert.assertEquals(1.0f, data[offset+0]);
		Assert.assertEquals(2.0f, data[offset+1]);
		Assert.assertEquals(3.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
		
	}
	
	
	@Test
	public void testRootOrientation() {
		float [] data = getData();
		int offset = MocapTextureEncoder.ROOT_ORIENTATION_OFFSET;
		
		Assert.assertEquals(5.0f, data[offset+0]);
		Assert.assertEquals(6.0f, data[offset+1]);
		Assert.assertEquals(7.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
	}
	
	
	
	@Test
	public void testNumberOfBones() {
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		int offset = encoder.getMaxRowSize();
		
		Assert.assertEquals(30.0f, data[offset+0]);
		Assert.assertEquals(0.0f, data[offset+1]);
		Assert.assertEquals(0.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		 
	}
	
	
	@Test
	public void testLHipJointDefinition() {
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		int offset = encoder.getMaxRowSize() + 4;
		
		// id and parent id
		Assert.assertEquals(1.0f, data[offset+0]);
		Assert.assertEquals(0.0f, data[offset+1]);
		Assert.assertEquals(0.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
		// direction vector
		Assert.assertEquals(0.734638f, data[offset+4]);
		Assert.assertEquals(-0.55547f, data[offset+5]);
		Assert.assertEquals(0.389564f, data[offset+6]);
		Assert.assertEquals(0.0f, data[offset+7]);
		
		// bone length
		Assert.assertEquals(2.86753f, data[offset+8]);
		Assert.assertEquals(0.0f, data[offset+9]);
		Assert.assertEquals(0.0f, data[offset+10]);
		Assert.assertEquals(0.0f, data[offset+11]);
		
		// axis orientation
		Assert.assertEquals(1.1f, data[offset+12]);
		Assert.assertEquals(2.6546f, data[offset+13]);
		Assert.assertEquals(3.654f, data[offset+14]);
		Assert.assertEquals(0.0f, data[offset+15]);
		
		// axis order of initial rotations
		Assert.assertEquals(1.0f, data[offset+16]);
		Assert.assertEquals(2.0f, data[offset+17]);
		Assert.assertEquals(3.0f, data[offset+18]);
		Assert.assertEquals(0.0f, data[offset+19]);
		
		// dof
		Assert.assertEquals(0.0f, data[offset+20]);
		Assert.assertEquals(0.0f, data[offset+21]);
		Assert.assertEquals(0.0f, data[offset+22]);
		Assert.assertEquals(0.0f, data[offset+23]);
		
		Assert.assertEquals(0.0f, data[offset+24]);
		Assert.assertEquals(0.0f, data[offset+25]);
		Assert.assertEquals(0.0f, data[offset+26]);
		Assert.assertEquals(0.0f, data[offset+27]);
		
		// limits
		Assert.assertEquals(0.0f, data[offset+28]);
		Assert.assertEquals(0.0f, data[offset+29]);
		Assert.assertEquals(0.0f, data[offset+30]);
		Assert.assertEquals(0.0f, data[offset+31]);
		
		Assert.assertEquals(0.0f, data[offset+32]);
		Assert.assertEquals(0.0f, data[offset+33]);
		Assert.assertEquals(0.0f, data[offset+34]);
		Assert.assertEquals(0.0f, data[offset+35]);
		
		Assert.assertEquals(0.0f, data[offset+36]);
		Assert.assertEquals(0.0f, data[offset+37]);
		Assert.assertEquals(0.0f, data[offset+38]);
		Assert.assertEquals(0.0f, data[offset+39]);
		
		Assert.assertEquals(0.0f, data[offset+40]);
		Assert.assertEquals(0.0f, data[offset+41]);
		Assert.assertEquals(0.0f, data[offset+42]);
		Assert.assertEquals(0.0f, data[offset+43]);
	}
	
	
	
	@Test
	public void testRFemurDefinition() {
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		int offset = encoder.getMaxRowSize() + encoder.getBoneDefinitionSize() * 6 + 4;
		
		// id and parent id
		Assert.assertEquals(7.0f, data[offset+0]);
		Assert.assertEquals(6.0f, data[offset+1]);
		Assert.assertEquals(0.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
		// direction vector
		Assert.assertEquals(-0.34202f, data[offset+4]);
		Assert.assertEquals(-0.939693f, data[offset+5]);
		Assert.assertEquals(0.0f, data[offset+6]);
		Assert.assertEquals(0.0f, data[offset+7]);
		
		// bone length
		Assert.assertEquals(6.12033f, data[offset+8]);
		Assert.assertEquals(0.0f, data[offset+9]);
		Assert.assertEquals(0.0f, data[offset+10]);
		Assert.assertEquals(0.0f, data[offset+11]);
		
		// axis orientation
		Assert.assertEquals(0.0f, data[offset+12]);
		Assert.assertEquals(0.0f, data[offset+13]);
		Assert.assertEquals(-20.0f, data[offset+14]);
		Assert.assertEquals(0.0f, data[offset+15]);
		
		// axis order of initial rotations
		Assert.assertEquals(1.0f, data[offset+16]);
		Assert.assertEquals(2.0f, data[offset+17]);
		Assert.assertEquals(3.0f, data[offset+18]);
		Assert.assertEquals(0.0f, data[offset+19]);
		
		// dof
		Assert.assertEquals(4.0f, data[offset+20]);
		Assert.assertEquals(5.0f, data[offset+21]);
		Assert.assertEquals(6.0f, data[offset+22]);
		Assert.assertEquals(0.0f, data[offset+23]);
		
		Assert.assertEquals(0.0f, data[offset+24]);
		Assert.assertEquals(0.0f, data[offset+25]);
		Assert.assertEquals(0.0f, data[offset+26]);
		Assert.assertEquals(0.0f, data[offset+27]);
		
		// limits
		Assert.assertEquals(-160.0f, data[offset+28]);
		Assert.assertEquals(20.0f, data[offset+29]);
		Assert.assertEquals(-70.0f, data[offset+30]);
		Assert.assertEquals(70.0f, data[offset+31]);
		
		Assert.assertEquals(-70.0f, data[offset+32]);
		Assert.assertEquals(60.0f, data[offset+33]);
		Assert.assertEquals(0.0f, data[offset+34]);
		Assert.assertEquals(0.0f, data[offset+35]);
		
		Assert.assertEquals(0.0f, data[offset+36]);
		Assert.assertEquals(0.0f, data[offset+37]);
		Assert.assertEquals(0.0f, data[offset+38]);
		Assert.assertEquals(0.0f, data[offset+39]);
		
		Assert.assertEquals(0.0f, data[offset+40]);
		Assert.assertEquals(0.0f, data[offset+41]);
		Assert.assertEquals(0.0f, data[offset+42]);
		Assert.assertEquals(0.0f, data[offset+43]);
	}
	
	
	@Test
	public void testNumberOfAnimations() {
		
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		int offset = encoder.getMaxRowSize() * 2;
		
		Assert.assertEquals(1.0f, data[offset]);
		Assert.assertEquals(0.0f, data[offset+1]);
		Assert.assertEquals(0.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
	}
	
	
	@Test
	public void testAnimation() {
		MocapTextureEncoder encoder = createEncoder();
		float [] data = encoder.encode();
		int offset = encoder.getMaxRowSize() * 3;
		
		// number of frames and units of rotation
		Assert.assertEquals(523.0f, data[offset+0]);
		Assert.assertEquals(1.0f, data[offset+1]);
		Assert.assertEquals(0.0f, data[offset+2]);
		Assert.assertEquals(0.0f, data[offset+3]);
		
		
		// root data - first frame
		Assert.assertEquals(0.0265202f, data[offset+4]);
		Assert.assertEquals(16.2134f, data[offset+5]);
		Assert.assertEquals(-25.4911f, data[offset+6]);
		Assert.assertEquals(0.0f, data[offset+7]);
		
		Assert.assertEquals(0.434917f, data[offset+8]);
		Assert.assertEquals(-2.05064f, data[offset+9]);
		Assert.assertEquals(5.16773f, data[offset+10]);
		Assert.assertEquals(0.0f, data[offset+11]);
		
		
		// lower back - first frame
		Assert.assertEquals(11.0f, data[offset+12]);
		Assert.assertEquals(0.0f, data[offset+13]);
		Assert.assertEquals(0.0f, data[offset+14]);
		Assert.assertEquals(0.0f, data[offset+15]);
		
		Assert.assertEquals(13.8853f, data[offset+16]);
		Assert.assertEquals(0.636769f, data[offset+17]);
		Assert.assertEquals(-9.00563f, data[offset+18]);
		Assert.assertEquals(0.0f, data[offset+19]);
		
		Assert.assertEquals(0.0f, data[offset+20]);
		Assert.assertEquals(0.0f, data[offset+21]);
		Assert.assertEquals(0.0f, data[offset+22]);
		Assert.assertEquals(0.0f, data[offset+23]);
		
		// lhand - first frame
		Assert.assertEquals(21.0f, data[offset+216]);
		Assert.assertEquals(0.0f, data[offset+217]);
		Assert.assertEquals(0.0f, data[offset+218]);
		Assert.assertEquals(0.0f, data[offset+219]);
		
		Assert.assertEquals(-6.29368f, data[offset+220]);
		Assert.assertEquals(19.1013f, data[offset+221]);
		Assert.assertEquals(0.0f, data[offset+222]);
		Assert.assertEquals(0.0f, data[offset+223]);
		
		Assert.assertEquals(0.0f, data[offset+224]);
		Assert.assertEquals(0.0f, data[offset+225]);
		Assert.assertEquals(0.0f, data[offset+226]);
		Assert.assertEquals(0.0f, data[offset+227]);
		
		// lclavicle - second frame
		offset += encoder.getFrameSize();
		Assert.assertEquals(17.0f, data[offset+168]);
		Assert.assertEquals(0.0f, data[offset+169]);
		Assert.assertEquals(0.0f, data[offset+170]);
		Assert.assertEquals(0.0f, data[offset+171]);
		
		Assert.assertEquals(-1.22253e-014f, data[offset+172]);
		Assert.assertEquals(-7.15625e-015f, data[offset+173]);
		Assert.assertEquals(0.0f, data[offset+174]);
		Assert.assertEquals(0.0f, data[offset+175]);
		
		Assert.assertEquals(0.0f, data[offset+176]);
		Assert.assertEquals(0.0f, data[offset+177]);
		Assert.assertEquals(0.0f, data[offset+178]);
		Assert.assertEquals(0.0f, data[offset+179]);
		
	}
	
	
}
